<template>
  <div>
    <group label-width="4.5em" label-margin-right="2em" label-align="right">
      <selector title="考试名称" placeholder="请选择考试名称" :options="examList" v-model="examName"></selector>
      <cell title="发送对象" is-link>共{{wxNameList.length}}人</cell>
      <x-textarea :max="200" placeholder="请输入发送的内容" v-model="content" @on-focus="onEvent('focus')" @on-blur="onEvent('blur')" :height="200" :rows="8" :cols="30"></x-textarea>
      <x-button type="warn">预览</x-button>
      <x-button type="primary" @click.native="sendMsg">发送</x-button>
    </group>
  </div>
</template>
<script>
  import { Selector, Group, Cell, XTextarea, XButton } from 'vux'

  export default {
    components: {
      Selector,
      Group,
      Cell,
      XTextarea,
      XButton
    },
    created: function () {
      this.$http.get('/jeewx/examController.do?getExamList')
        .then(function (res) {
          let resultList = res.body.obj
          let examList = []
          let examMap = new Map()
          console.log(resultList)
          resultList.forEach(function (value, index, array) {
            examList.push(value.examname)
            examMap.set(value.examname, value.examid)
          })
          this.examList = examList
          this.examName = this.examList[0]
          this.content = '尊敬的家长，您好。您孩子的' + this.examName + '考试成绩查询请点击：'
          this.$http.get('/jeewx/scoreController.do?getScoreListByExamId&examId=' + examMap.get(this.examName))
            .then(function (res) {
              this.scoreList = res.body.obj
            })
        })
      if (localStorage.studentList !== undefined) {
        let studentList = JSON.parse(localStorage.studentList)
        var wxNameArray = []
        studentList.forEach(function (value, index, array) {
          if (value.bindName !== '未绑定') {
            var a = value.bindName.split(',')
            a.forEach(function (arrayValue, index, array) {
              wxNameArray.push({
                studentId: value.studentId,
                wxName: arrayValue
              })
            })
          }
        })
        this.wxNameList = wxNameArray
        console.log(wxNameArray)
      }
    },
    methods: {
      onEvent (event) {
        console.log('on', event)
      },
      sendMsg () {
        console.log(this.wxNameList)
        this.wxNameList.forEach((value, index, array) => {
          let studentId = value.studentId
          console.log(localStorage.uuid)
          let content = this.content + ' http://canxin.mynatapp.cc/dist/#/StudentExamList?studentid=' + studentId
          this.$http.post('/wx/WxRobotController.do?sendWxMsgByRemarkName',
            {'uuid': localStorage.uuid, 'content': content, 'sendToUser': value.wxName})
          .then(function (res) {
            console.log(res)
          })
        })
      }
    },
    data () {
      return {
        content: '',
        examMap: Object,
        examName: String,
        examList: [],
        scoreList: [],
        wxNameList: [],
        studentList: []
      }
    }
  }
</script>

<style scoped>

</style>
